/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wiiscript;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import wiiremotej.WiiRemote;
import wiiremotej.WiiRemoteJ;

/**
 *
 * @author Evan
 */
public class WiiRemoteInterface{

    private WiiRemote remote;

    public void connect(WiiConnectedListener completed) {
        System.setProperty("bluecove.jsr82.psm_minimum_off", "true");
        ConnectThread thread = new ConnectThread(completed);
        thread.start();
    }
    public boolean disconnect() {
        if(remote != null) {
            remote.disconnect();
            return true;
        } else {
            return false;
        }
    }
    
    public boolean isConnected() {
        return (remote != null);
    }
    public WiiRemote getRemote() {
        return remote;
    }
    private class ConnectThread extends Thread {
        private WiiConnectedListener completed;
        ConnectThread(WiiConnectedListener c) {
            completed = c;
        }
        @Override
        public void run() {
            try {
                remote = WiiRemoteJ.findRemote();
            } catch (IllegalStateException ex) {
                Logger.getLogger(WiiRemoteInterface.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(WiiRemoteInterface.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(WiiRemoteInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
        if(remote != null) {
                try {
                    remote.setLEDIlluminated(0, true);
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(WiiRemoteInterface.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(WiiRemoteInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            completed.actionPerformed(true);
        } else {
            completed.actionPerformed(false);
        }
        }
    }
}
